/**
 * Created by sky on 2019/3/28.
 */

layui.define(['mobile', 'zepto'], function (exports) {
    var mobile = layui.mobile
        , layim = mobile.layim
        , layer = mobile.layer;

    var $ = layui.zepto;

    var PAY_TYPE = {
        Alipay: 1, //支付宝充值
        WeChat: 2, //微信充值
        Bank: 3, //银行卡充值
        Flower: 4, //花呗充值
        CreditAlipay: 5, //信用卡（支）
        CreditWechat: 6, //信用卡（微）
        AlipayAuto: 7, //支付宝自助
        WechatAuto: 8, //微信自助
        BankAuto: 9 //银行卡自助
    };

    var PAY_TYPE_NAME = {
        1: "支付宝充值",
        2: "微信充值",
        3: "花呗充值",
        4: "信用卡充值",
        5: "银行卡充值",
    };

    var _getPayTypeDetail = function (data) {
        var payTypeId = data.payTypeId;
        var payType = data.payType;
        var payUrl = data.payUrl;
    };

    var _getPayOrderDetail = function (data) {
        var orderId = data.orderId;
        var payType = data.payType;
        var payUrl = data.payUrl;
    };

    var _sendPayAmount = function (socketMgr, message) {
        var cid = message.cid;
        var topic_id = message.id; //对方的信息
        var receiptAddressId = message.receiptAddressId;
        var payType = message.payType;
        var amount = message.payAmount;
        var obj = {
            "type": "pay:select_pay_amount",
            "data": {
                "receiptAddressId": parseInt(receiptAddressId), // 收款方式id, 从pay:get_pay_type_amount_level中取receiptAddressId
                "payType": parseInt(payType), // 收款方式id, 从pay:get_pay_type_amount_level中取receiptAddressId
                "amount": parseInt(amount)
            }
        };
        var data = {
            ClientID: cid,
            MsgType: 10,
            Topic: {
                TOPIC_TYPE: 0,
                ID: topic_id
            },
            Info: JSON.stringify(obj)
        };
        console.log("justchat=》send data:" + JSON.stringify(data));
        socketMgr.send(5, "ChatReq", data);
    };

    var _sendPayType = function (socketMgr, message) {
        var cid = message.cid;
        var topic_id = message.id; //对方的信息
        var payType = message.paytype;
        var obj = {
            "type": "pay:select_pay_type",
            "data": {
                "payType": parseInt(payType)
            }
        };
        var data = {
            ClientID: cid,
            MsgType: 10,
            Topic: {
                TOPIC_TYPE: 0,
                ID: topic_id
            },
            Info: JSON.stringify(obj)
        };
        socketMgr.send(5, "ChatReq", data);
    };

    var justchatCustom = {
        _socketMgr: null,
        initCustom: function (socketMgr) {
            this._socketMgr = socketMgr;
        },
        getMessage: function (msg) {
            var msg_type = msg.MsgType;
            var info = msg.Info;
            if (msg_type == "CUSTOM") {
                var json_info = JSON.parse(info);
                var type = json_info.type;
                var data = json_info.data;
                if (type == "pay:get_pay_type_amount_levels") {
                    //_getPayTypeAmountLevels(data);
                } else if (type == "pay:get_pay_type_detail") {
                    _getPayTypeDetail(data);
                } else if (type == "get_pay_order_detail") {
                    _getPayOrderDetail(data);
                }
            }
        },
        sendMessage: function (socketMgr, res) {
            var type = res.type;
            if (type == "pay:select_pay_type") {
                _sendPayType(socketMgr, res.message);
            } else if (type == "pay:select_pay_amount") {
                _sendPayAmount(socketMgr, res.message);
            }
        },
        renderPayTypes: function (target_id) {
            var self = this;
            layui.config({
                base: '../../script/'
            }).use(['justchatApiMgr'], function () {
                var justchatApiMgr = layui.justchatApiMgr;
                var onSucceedCallback = function (result) {
                    var status = result.status;
                    var data = result.data;
                    if (status == 0) {
                        var payTypes = data.payTypes;
                        if(payTypes){
                            setTimeout(function(){
                                self.addHtmlToChat(payTypes);
                            }, 600);
                        }
                    }
                };
                var onErrorCallback = function () {
                    $('.layim-chat-main').addClass("agencymain");
                    $('.layim-chat-main').removeClass("just-chat-main-pay");
                };
                justchatApiMgr.playerGetPayTypes(target_id, onSucceedCallback, onErrorCallback);
            });
        },
        addHtmlToChat: function (payTypes) {
            var colors = ['#508ae7','#06e104','#58a0eb','#d3804b','#e1af4f'];
            var self = this;
            var html = '';
                for (var i = 0; i < payTypes.length; i++) {
                var payType = payTypes[i];
                    html += '<button style="color:#fff;background:'+colors[payType-1]+'" layim-event="sendPayTypeMessage" payType="' + payType + '" class="layui-btn layui-btn-primary layui-btn-sm layui-btn-radius">' + PAY_TYPE_NAME[payType] + '</button>';

                    if(i==payTypes.length-1){
                        html += '<button style="color:#fff;background:pink;" layim-event="send" SendMessage="我充好了" class="layui-btn layui-btn-primary layui-btn-sm layui-btn-radius">我充好了</button>';
                    }
            }
            layim.renderPayTypes(html);
        }

    };

    exports('justchatCustom', justchatCustom);
});
